package cn.chy.com.springbootdemo2.advice;

import cn.chy.com.springbootdemo2.exception.UnifyException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolationException;
import java.util.List;

/**
 * @Description: TODO
 * @author: 颜景琦
 * @date: 2021年10月18日 14:34
 */
@ControllerAdvice
public class GlobalExceptionConfig {


    //    @Resource
//    private ExceptionConfig exceptionConfig;
//
//    @ExceptionHandler({Exception.class})
//    public ResponseEntity<UnifyException> handlerException(HttpServletRequest request,
//                                                           Exception e) {
//        String url = request.getRequestURL().toString();
//        String method = request.getMethod();
//        String result = url + "   " + method;
//
//
//        UnifyException unifyException = UnifyException.builder().
//                code(99999).message("服务器局部报错").requestUrl(result).build();
//
//        HttpHeaders httpHeaders = new HttpHeaders();
//        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
//        HttpStatus resolve = HttpStatus.resolve(500);
//
//
//        ResponseEntity responseEntity = new ResponseEntity(unifyException, httpHeaders, resolve);
//
//        return responseEntity;
//    }
//
//
//    @ExceptionHandler(value = HttpException.class)
//    public ResponseEntity<UnifyException> handlerHttpException(HttpServletRequest request,
//                                                               HttpException e) {
//
//
//        System.out.println("进入了此方法");
//        String url = request.getRequestURL().toString();
//        String method = request.getMethod();
//        String result = url + "   " + method;
//
//        System.out.println(e.getCode());
//        System.out.println(e.getHttpStatusCode());
//        System.out.println("---");
//        System.out.println(exceptionConfig.getMessage(20001));
//
//        UnifyException unifyException = UnifyException.builder().
//                code(e.getCode())
//                .message(exceptionConfig.getMessage(20002)).
//                        requestUrl(result).build();
//
//
//        HttpHeaders httpHeaders = new HttpHeaders();
//        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
//        HttpStatus httpStatus = HttpStatus.resolve(e.getHttpStatusCode());
//
//
//        ResponseEntity responseEntity = new ResponseEntity(unifyException, httpHeaders, httpStatus);
//
//        return responseEntity;
//    }
//
//
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public UnifyException handlerMethodArgumentNotValidException(HttpServletRequest request,
                                                                 MethodArgumentNotValidException e) {

        String url = request.getMethod() + "=" + request.getRequestURL();


        List<ObjectError> allErrors = e.getAllErrors();
        String buffer = "";

        for (ObjectError allError : allErrors) {
            buffer += allError.getDefaultMessage() + ";";
        }


        UnifyException responseEntity = UnifyException.builder().
                code(10001).message(buffer).requestUrl(url).build();

        return responseEntity;
    }

    @ExceptionHandler(value = ConstraintViolationException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public UnifyException handlerConstraintViolationException(HttpServletRequest request,
                                                              ConstraintViolationException e) {

        String url = request.getMethod() + "=" + request.getRequestURL();

        String message = e.getMessage();


        UnifyException responseEntity = UnifyException.builder().
                code(10003).message(message).requestUrl(url).build();

        return responseEntity;
    }
}
